package com.xudu.tourbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xudu.tourbackend.common.globalEnum.ErrorCode;
import com.xudu.tourbackend.entity.ScenicArea;
import com.xudu.tourbackend.entity.ScenicSpot;
import com.xudu.tourbackend.entity.request.AddScenicSpotRequest;
import com.xudu.tourbackend.entity.request.SearchScenicSpot;
import com.xudu.tourbackend.entity.request.UpdateScenicSpotRequest;
import com.xudu.tourbackend.entity.vo.ScenicSpotVo;
import com.xudu.tourbackend.exception.ServiceException;
import com.xudu.tourbackend.mapper.ScenicAreaMapper;
import com.xudu.tourbackend.mapper.ScenicSpotMapper;
import com.xudu.tourbackend.service.IScenicSpotService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xudu.tourbackend.service.IUserService;
import com.xudu.tourbackend.utils.QiniuUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xudu
 * @since 2024-06-17
 */
@Service
public class ScenicSpotServiceImpl extends ServiceImpl<ScenicSpotMapper, ScenicSpot> implements IScenicSpotService {

    @Resource
    private ScenicSpotMapper scenicSpotMapper;

    @Resource
    private ScenicAreaMapper scenicAreaMapper;

    @Resource
    private IUserService userService;

    @Resource
    private QiniuUtil qiniuUtil;


    @Override
    public int addScenicSpot(@Valid AddScenicSpotRequest addScenicSpotRequest, HttpServletRequest request) {
        Boolean b = userService.isAdmin(request);
        if (!b){
            throw new ServiceException(ErrorCode.NO_AUTH);
        }
        //判断是否存在该景点对应的景区
        Integer scenicAreaId = addScenicSpotRequest.getScenicAreaId();
        ScenicArea scenicArea = scenicAreaMapper.selectById(scenicAreaId);
        if (scenicArea == null){
            throw new ServiceException(ErrorCode.PARAMS_ERROR);
        }

        String scenicSpotName = addScenicSpotRequest.getScenicSpotName();
        MultipartFile scenicSpotImage = addScenicSpotRequest.getScenicSpotImage();
        String scenicSpotInfo = addScenicSpotRequest.getScenicSpotInfo();
        //todo限制输入的景点信息长度

        ScenicSpot scenicSpot = new ScenicSpot();
        scenicSpot.setScenicSpotName(scenicSpotName);
        scenicSpot.setScenicSpotImage(qiniuUtil.upload(scenicSpotImage));
        scenicSpot.setScenicSpotInfo(scenicSpotInfo);
        scenicSpot.setScenicAreaId(scenicAreaId);

        boolean save = this.save(scenicSpot);
        if (!save){
            throw new ServiceException(ErrorCode.SYSTEM_ERROR);
        }

        return scenicSpot.getId();
    }

    @Override
    public int deleteScenicSpot(List<Integer> ids, HttpServletRequest request) {
        if (ids.isEmpty()){
            throw new ServiceException(ErrorCode.PARAMS_ERROR);
        }
        for (Integer id : ids){
            if (id <= 0){
                throw new ServiceException(ErrorCode.PARAMS_ERROR);
            }
        }

        if (!userService.isAdmin(request)){
            throw new ServiceException(ErrorCode.NO_AUTH);
        }

        return scenicSpotMapper.deleteBatchIds(ids);

    }

    @Override
    public Boolean updateScenicSpot(UpdateScenicSpotRequest updateScenicSpotRequest, HttpServletRequest request) {
        //判断是否为管理员
        if (!userService.isAdmin(request)){
            throw new ServiceException(ErrorCode.NO_AUTH);
        }
        //判断要修改的景点是否存在
        Integer id = updateScenicSpotRequest.getId();
        if (id <= 0 || scenicSpotMapper.selectById(id) == null){
            throw new ServiceException(ErrorCode.PARAMS_ERROR);
        }

        ScenicSpot scenicSpot = new ScenicSpot();
        BeanUtils.copyProperties(updateScenicSpotRequest, scenicSpot);

        if (updateScenicSpotRequest.getScenicSpotImage() != null){
            scenicSpot.setScenicSpotImage(qiniuUtil.upload(updateScenicSpotRequest.getScenicSpotImage()));
        }

        //如果要修改对应的景区id，则需要判断该景区是否存在
        if (scenicSpot.getScenicAreaId() != null){
            ScenicArea scenicArea = scenicAreaMapper.selectById(scenicSpot.getScenicAreaId());
            if (scenicArea == null){
                throw new ServiceException(ErrorCode.PARAMS_ERROR);
            }
        }

        //todo 限制输入的景点信息长度

        return this.updateById(scenicSpot);

    }

    @Override
    public IPage<ScenicSpotVo> searchScenicSpot(SearchScenicSpot searchScenicSpot){
        if (searchScenicSpot == null){
            throw new ServiceException(ErrorCode.PARAMS_ERROR);
        }

        //组合条件查询
        QueryWrapper<ScenicSpot> queryWrapper = new QueryWrapper<>();
        Integer id = searchScenicSpot.getId();
        if (id != null && id > 0){
            queryWrapper.lambda().eq(ScenicSpot::getId, id);
        }
        String scenicSpotName = searchScenicSpot.getScenicSpotName();
        if (StringUtils.isNotBlank(scenicSpotName)){
            queryWrapper.lambda().like(ScenicSpot::getScenicSpotName, scenicSpotName);
        }
        String scenicSpotInfo = searchScenicSpot.getScenicSpotInfo();
        if (StringUtils.isNotBlank(scenicSpotInfo)){
            queryWrapper.lambda().like(ScenicSpot::getScenicSpotInfo, scenicSpotInfo);
        }
        Integer scenicAreaId = searchScenicSpot.getScenicAreaId();
        if (scenicAreaId != null && scenicAreaId > 0){
            queryWrapper.lambda().eq(ScenicSpot::getScenicAreaId, scenicAreaId);
        }

        Page<ScenicSpot> page = new Page<>(searchScenicSpot.getPageNum(), searchScenicSpot.getPageSize());
        Page<ScenicSpot> scenicSpotPage = this.page(page, queryWrapper);
        if (scenicSpotPage.getTotal() == 0){
            throw new ServiceException(ErrorCode.PARAMS_ERROR);
        }

        //将查询到的景点信息列表转换为vo类型
        ArrayList<ScenicSpotVo> ScenicSpotVoList = new ArrayList<>();
        scenicSpotPage.getRecords().forEach(scenicSpot -> {
            ScenicSpotVo scenicSpotVo = new ScenicSpotVo();
            BeanUtils.copyProperties(scenicSpot, scenicSpotVo);
            ScenicSpotVoList.add(scenicSpotVo);
        });
        Page<ScenicSpotVo> scenicSpotVoPage = new Page<>(scenicSpotPage.getCurrent(), scenicSpotPage.getSize(), scenicSpotPage.getTotal());
        scenicSpotVoPage.setRecords(ScenicSpotVoList);
        return scenicSpotVoPage;

    }

    @Override
    public List<ScenicSpotVo> searchScenicSpot(String scenicSpotName) {

        QueryWrapper<ScenicSpot> queryWrapper = new QueryWrapper<>();
        if (scenicSpotName != null){
            queryWrapper.lambda().like(ScenicSpot::getScenicSpotName, scenicSpotName);
        }
        List<ScenicSpot> scenicSpots = scenicSpotMapper.selectList(queryWrapper);
        if (scenicSpots == null || scenicSpots.isEmpty()){
            return Collections.emptyList();
        }

        //return scenicSpots.stream().map(scenicSpot -> {
        //    ScenicSpotVo scenicSpotVo = new ScenicSpotVo();
        //    BeanUtils.copyProperties(scenicSpot, scenicSpotVo);
        //    return scenicSpotVo;
        //}).toList();
        ArrayList<ScenicSpotVo> list = new ArrayList<>();
        scenicSpots.forEach(scenicSpot -> {
            ScenicSpotVo scenicSpotVo = new ScenicSpotVo();
            BeanUtils.copyProperties(scenicSpot, scenicSpotVo);
            list.add(scenicSpotVo);
        });
        return list;
    }


}
